var debug_ip = "http://192.168.199.111/";
jQuery(function($) {

	/*获取元素*/
	var qs = {
		regbtn: $(".user .register"),
		regBox: $(".register_box"),
		logbtn: $(".user .login"),
		logBox: $(".login_box"),
		paw: $(".paw_error"),
		phone: $(".register_box .phone_error"),
		dl: $(".user dl"),
		more: $(".user .more"),
		verifyText: $(".verify input"),
		verifyBtn: $("#verify button")
	};
	
	var timers = {
		timer: 0,
		verifyTimer: 60
	}
	
	/*工具函数*/
	var utils = {
		type: function(value) {
			return $.type(value);
		},
		isNull: function(value) {
			return  (value !== null && value !== "");
		} 
	}
	var	$doc = $(document),
		doc = document;
	
	/*跳转页面*/
	function redirect(num) {
		doc.location.href = "http://192.168.199.206/#p=（会员）填写资料页面";
	}
	 
	/*请求非法处理*/
	function illegal(msg) {
		alert(msg);
	}
	
	/*手机号码错误*/
	function phone_error(mes) {
		qs.phone.html(mes);
	}
	
	/*密码错误*/
	function paw_error(msg) {
		qs.paw.html(msg);
	}
	
	/*用户注册*/
	function submit_register() {
		var mobile = $(".phone").val();
		var pwd = $(".password").val();
			
		//检查数据是否异常 
		if(!utils.isNull(mobile)
		|| mobile.substring(0,1) != "1"
		|| mobile.length != 11)	{
			var mes = " 请填写正确的手机号码！"
			phone_error(mes);
		}
		
		if(!utils.isNull(pwd))	{
			var msg = " 密码不能为空！";
			paw_error(msg);			
			return false;
		}

		$.ajax({
				type: "POST",
				url: "../control/doaction.php",
				dataType: "text",
				data: { 
					action:"user",
					job:'reg',
					mobile:mobile,
					pwd:$.md5(pwd)
				},
				timeout: 1000,
				complete: function(){},
				success: function(data)	{
					var obj = eval( "(" + data + ")" );
					switch(obj.ret) {
						case 0:
							redirect(); //跳转
							break;
						case 1:
							illegal("非法请求"); //非法请求
							break;
						case 2:
							phone_error("手机号码不正确"); //手机号码不正确
							break;
						case 3:
							paw_error("密码错误"); //密码错误
							break;
					}
				}
		});	
	}
	
	/*倒计时*/
	function timeout(obj) {
		clearInterval(timers.timer);
		timers.timer = setInterval(function() {
			var time = timers.verifyTimer;
			qs.verifyBtn.html(timers.verifyTimer--);
			if (time === 0) {				
				qs.verifyBtn.html("发送验证码");
				qs.verifyBtn.attr("disabled", false);
				clearInterval(timers.timer);		
			}				
		}, 100);
	}
	
	/*验证码失效*/
	function badCode() {
		console.log(124);
	}
	
	/*手机验证*/
	function verify() {console.log(1);
		$.ajax({
			type: "post",
			url: "../control/doaction.php",
			dataType: "text",
			data: {
				action: "user",
				job : "smsverify"
			},
			timeout: 1000,
			success: function(data) {
				var code = eval( "(" + data + ")" );
				
				switch(code.ret) {
					case 60: 
						timers.verifyTimer = code.ret;
						qs.verifyBtn.attr("disabled", true);
						timeout(); //设置超时
						break;
					case -1:
						badCode(); //验证码失效
						break;
				}
			}
		});
	}
	
	function events() {
		$doc.on("click", ".user .register", function() {
			qs.regBox.show();
			qs.logBox.hide();
		});
	
		$doc.on("click", ".register_box .close", function() {
			qs.regBox.hide();
		});
		$doc.on("click", ".user .login", function() {
			qs.logBox.show();
			qs.regBox.hide();
		});
		$doc.on("click", ".login_box .close", function() {
			qs.logBox.hide();
		});
		$doc.on("click", ".register_box .submit", function() {
			submit_register();
		});
		
		
		$doc.on("click", ".login_now a", function() {
			qs.logBox.show();
			qs.regBox.hide();
		});
		
		//手机端弹出mini导航栏
		$doc.on("click", ".user .more", function() {
			$(".user div dl").toggle();
		});
		$doc.on("click", "#verify button", function(event) {
			event.preventDefault();
			verify();
		});
		
		//取消a标签默认行为
		$doc.on("click", ".user .register", function(event) {
			event.preventDefault();
		});
		
		//取消a标签默认行为
		$doc.on("click", ".user .login", function(event) {
			event.preventDefault();
		});
	}
	events();
});